package tron;

import java.awt.*;
import javax.swing.*;

public class FieldPanel extends JPanel {
    public Field field;
    private int sizeOfPixel = 10;
    private Color colors[];

    public FieldPanel(Field field) {
	this.field = field;
	initColors();
	Dimension sizeOfField = new Dimension((field.getSize() + 1)
		* sizeOfPixel, (field.getSize() + 1) * sizeOfPixel);
	setPreferredSize(sizeOfField);
	setBackground(Color.BLACK);
    }

    public void paintComponent(Graphics graphics) {
	super.paintComponent(graphics);
	for (int i = 1; i <= field.getSize(); ++i)
	    for (int j = 1; j <= field.getSize(); ++j) {
		int cell = field.get(i, j);
		if (cell > 0)
		    drawCircle(graphics, colors[cell], (i - 1) * sizeOfPixel,
			    (j - 1) * sizeOfPixel);
		else if (cell == -1) {
		    drawCircle(graphics, Color.WHITE, (i - 1) * sizeOfPixel,
			    (j - 1) * sizeOfPixel);
		}
	    }
	drawSides(graphics);
    }

   
    private void drawCircle(Graphics graphics, Color color, int i, int j) {
	graphics.setColor(color);
	graphics.fillOval(i, j, sizeOfPixel, sizeOfPixel);
    }

    private void initColors() {
	colors = new Color[5];
	colors[1] = Color.BLUE;
	colors[2] = Color.RED;
	colors[3] = Color.GREEN;
	colors[4] = Color.YELLOW;
    }

    private void drawSides(Graphics graphics) {
	graphics.setColor(Color.RED);
	int max = sizeOfPixel * field.getSize();
	graphics.drawLine(0, 0, 0, max);
	graphics.drawLine(0, 0, max, 0);
	graphics.drawLine(max, 0, max, max);
	graphics.drawLine(0, max, max, max);
    }

}
